<?php
class Xuatxe extends Controller {
	private $data;

	function index() {
		$this -> loadTools();
		$this -> loadData();
		$this -> load -> view('xuatxe/view_nhapdulieu', $this -> data);
	}

	function loadTools() {
		$this -> load -> helper('form');
		$this -> load -> helper('url');
		$this -> load -> database();
	}

	function loadData() {
		$this -> data = array();
		
		$this -> data['so_khung'] = array (
		'name' => 'so_khung',
		'size' => '50'
		);
		$this -> data['so_may'] = array (
		'name' => 'so_may',
		'size' => '50'
		);
		$this -> data['error_message'] = '';
		$this -> data['title'] = 'Nhap so khung/so may';
	}

	function checkSKSM() {
		$sk = $_POST['so_khung'];
		$sm = $_POST['so_may'];

		$this -> load -> model('Xemay_items');

		//NOTE: existItem and da_ban must be boolean type !
		$existItem = $this -> Xemay_items -> existItem($sk,$sm,$da_ban);
		
		if ( $existItem && $da_ban==0) {
			$this -> load -> helper('url');
 			redirect('xuatxe/themThongTin/'.$sk.'/'.$sm.'/');
		} else {
			//die('HEre !');
			$this -> loadTools();
			$this -> loadData();	
			$this -> data['error_message'] = 'Strange !';
			if ( !$existItem ) {
				$this -> data['error_message'] = 'Khong ton tai so khung/so may, xin nhap lai!';
			} else {
				if ($da_ban==1) {
					$this -> data['error_message'] = 'Xe da ban ! Xin nhap SK/SM khac!';
				}
			}
			$this -> load -> view('xuatxe/view_nhapdulieu', $this -> data);
		}
	}
	
	function themThongTin($sk,$sm) {
		$this -> load -> model('Xemay_items');
		$this -> load -> model('Xemay');
		$this -> load -> helper('date');
		$this -> load -> helper('form');
		$this -> load -> helper('url');
		$this -> load -> helper('my_utilities');

		$row_item = $this -> Xemay_items -> getRow($sk,$sm);

		$this -> data['title'] = 'Nhap phieu xuat';
		
		$this -> data['so_khung'] = $sk;
		$this -> data['so_may'] = $sm;
		
		$row_cat = $this -> Xemay -> getRow($row_item -> CAT_ID);
		$this -> data['loaixe'] = $row_cat -> TEN . ' / '.$row_cat -> MAU;

		
		$this -> data['gia_xuat_form'] = array (
			'name' => 'gia_xuat',
			'value' => format_currency($this -> Xemay_items -> getSuggestedSellPrice($sk,$sm))
		);
		//TODO: get rid of comma by later processing - but be carefull, it may not contain comma

		$this -> data['ngay_xuat_form'] = array (
			'name' => 'ngay_xuat',
			'value' => getDateToday()
		);
		
		$this -> data['back_button_form'] = array (
			'name' => 'back_button',
			'onClick' => 'history.go(-1)'
		);
		
		//TODO: fill it with logged in user
		$this -> data['nguoi_xuat'] = 'lan';
		
		$this -> load -> view('xuatxe/view_themthongtin',$this -> data);		
	}
	
	function loadPOSTdata() {
		$this -> data['loaixe'] = $_POST['loaixe'];
		$this -> data['so_khung'] = $_POST['so_khung'];
		$this -> data['so_may'] = $_POST['so_may'];
		$this -> data['gia_xuat'] = $_POST['gia_xuat'];
		$this -> data['ngay_xuat'] = $_POST['ngay_xuat'];		
	}
	
	function preview() {
		$this -> load -> helper('form');
		
		$this -> data['title'] = 'Xem lai form xuat xe';
		$this -> loadPOSTdata();
		
		//TODO: logged in user
		$this -> data['nguoi_xuat'] = 'lan';
		
		$this -> data['back_button_form'] = 		$this -> data['back_button_form'] = array (
			'name' => 'back_button',
			'onClick' => 'history.go(-1)'
		);		
		
		$this -> load -> view('xuatxe/view_preview',$this -> data);						
	}
	
	function updateDB() {
		$this -> load -> helper('my_utilities');
		$this -> load -> model('Xemay_items');
		$this -> load -> helper('url');
		
		$sk = $_POST['so_khung'];
		$sm= $_POST['so_may'];
		
		//TODO: logged in user
		$nguoi_xuat = 'lan';
		
		$this -> Xemay_items -> updateItemBySold($sk,$sm,getRidComma($_POST['gia_xuat']),$_POST['ngay_xuat'],$nguoi_xuat);
		
		redirect('xuatxe');
	}
}
?>